/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ibmmodel3;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author mwh
 */
public class TranslationTableGrep implements TranslationTable {

    String filename;


    public TranslationTableGrep(String filename) {
        this.filename = filename;
    }


    public double get(Integer source, Integer target) {

        List<String> grepRes = myGrep("^" + source + " ");
        Map<Integer, Double> transProb = new HashMap<Integer, Double>();

        /**
         * See TranslationTableMem for decription of this smoothing
         */
        double minProb = Double.POSITIVE_INFINITY;
        for (String line : grepRes) {
            String[] tokens = line.split(" ");
            int t = Integer.parseInt(tokens[1]);
            double p = Double.parseDouble(tokens[2]);
            minProb = Math.min(minProb, p);
            transProb.put(t, p);
        }
        double unkProb = 0.5 * minProb;
        double subProb = unkProb / transProb.size();
        if (transProb.containsKey(target)) {
            return transProb.get(target) - subProb;
        } else {
            return unkProb;
        }
    }


    public Map<Integer, Double> get(Integer source) {

        List<String> grepRes = myGrep("^" + source + " ");
        Map<Integer, Double> transProb = new HashMap<Integer, Double>();

        /**
         * See TranslationTableMem for decription of this smoothing
         */
        double minProb = Double.POSITIVE_INFINITY;
        for (String line : grepRes) {
            String[] tokens = line.split(" ");
            int t = Integer.parseInt(tokens[1]);
            double p = Double.parseDouble(tokens[2]);
            minProb = Math.min(minProb, p);
            transProb.put(t, p);
        }
        double unkProb = 0.5 * minProb;
        double subProb = unkProb / transProb.size();
        for (Integer t : transProb.keySet()) {
            transProb.put(t, transProb.get(t) - subProb);
        }
        // Save prob for unknown word in -1024
        transProb.put(-1024, unkProb);
        return transProb;
    }


    public double getLog10(int source, int target) {
        return Math.log10(get(source, target));
    }


    private List<String> myGrep(String regExp) {
        List<String> res = new ArrayList<String>();
        try {
            String re = ".*" + regExp + ".*";
            BufferedReader input = new BufferedReader(new FileReader(filename));
            String line;
            boolean found = false;
            boolean foundEnd = false;
            while ((line = input.readLine()) != null && !foundEnd) {
                if (line.matches(re)) {
                    found = true;
                    res.add(line);
                } else {
                    if (found) {
                        foundEnd = true;
                    }
                }
            }
        } catch (IOException ex) {
            Logger.getLogger(TranslationTableGrep.class.getName()).log(Level.SEVERE, null, ex);
        }
        return res;
    }
}
